package com.atguigu.yygh.hosp.controller.admin;


import com.atguigu.yygh.common.result.R;
import com.atguigu.yygh.common.util.MD5;
import com.atguigu.yygh.hosp.service.HospitalSetService;
import com.atguigu.yygh.model.hosp.HospitalSet;
import com.atguigu.yygh.vo.hosp.HospitalSetQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;

/**
 * <p>
 * 医院设置表 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2023-05-14
 */
@Api(value = "医院设置接口", tags = "医院设置信息")
@RestController
@RequestMapping("/admin/hosp/hospitalSet")
public class AdminHospitalSetController {
    @Autowired
    private HospitalSetService hospitalSetService;

    @ApiOperation(value = "查询所有医院设置")
    @GetMapping("/findAll")
    public List<HospitalSet> findAll() {
        return hospitalSetService.list();
    }

    @ApiOperation(value = "医院设置删除")
    @DeleteMapping("/deleteById/{id}")
    public R deleteById(@PathVariable Integer id) {
        hospitalSetService.removeById(id);
        return R.ok();
    }

    @ApiOperation(value = "分页医院设置列表")
    @PostMapping("/page/{pageNum}/{size}")
    public R pageList(@ApiParam(name = "pageNum", value = "当前页码", required = true)
                      @PathVariable Integer pageNum,
                      @ApiParam(name = "size", value = "每页显示多少条")
                      @PathVariable Integer size,
                      @RequestBody HospitalSetQueryVo hospitalSetQueryVo
    ) {
        Page<HospitalSet> page = new Page<>(pageNum, size);
        QueryWrapper<HospitalSet> wrapper = new QueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(hospitalSetQueryVo.getHosname()),
                "hostname", hospitalSetQueryVo.getHosname());
        wrapper.eq(StringUtils.isNotBlank(hospitalSetQueryVo.getHoscode()),
                "hoscode", hospitalSetQueryVo.getHoscode());
        hospitalSetService.page(page, wrapper);

        return R.ok().data("total", page.getTotal()).data("rows", page.getRecords());
    }

    @ApiOperation(value = "新增医院设置")
    @PostMapping("/save")
    public R save(
            @ApiParam(name = "hospitalSet", value = "医院设置对象", required = true)
            @RequestBody HospitalSet hospitalSet) {
        //设置状态  1可使用、0不可使用
        hospitalSet.setStatus(1);
        //签名密钥
        Random random = new Random();
        hospitalSet.setSignKey(MD5.encrypt(System.currentTimeMillis() + "" + random.nextInt(1000)));
        hospitalSetService.save(hospitalSet);
        return R.ok();
    }

    @ApiOperation(value = "根据ID查询医院设置")
    @GetMapping("/getHospSet/{id}")
    public R getById(@ApiParam(name = "id", value = "医院设置", required = true)
                     @PathVariable String id
    ) {
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        return R.ok().data("item", hospitalSet);
    }

    @ApiOperation(value = "根据ID进行医院修改设置")
    @PostMapping("/updateHospSet")
    public R updateById(
            @ApiParam(name = "hospitalSet", value = "医院设置对象", required = true)
            @RequestBody HospitalSet hospitalSet) {
        hospitalSetService.updateById(hospitalSet);
        return R.ok();
    }

    //修改之回显数据
    @GetMapping("/detail/{id}")
    public R detail(@PathVariable Integer id) {
        return R.ok().data("item", hospitalSetService.getById(id));
    }

    //修改之修改
    @PutMapping("/update")
    public R update(@RequestBody HospitalSet hospitalSet) {
        hospitalSetService.updateById(hospitalSet);
        return R.ok();
    }
    //批量删除
    @ApiOperation(value = "批量删除")
    @DeleteMapping("/batchRemove")
    public R batchRemoveByIds(@RequestBody List<Long> ids) {
        hospitalSetService.removeByIds(ids);
        return R.ok();
    }

    //锁定和解锁
    //医院设置锁定和解锁
    @ApiOperation(value = "医院设置锁定和解锁")
    @PutMapping("/lockHospSet/{id}/{status}")
    public R lockHospSet(@PathVariable Long id,
                         @PathVariable Integer status) {
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        //设置状态
        hospitalSet.setStatus(status);
        hospitalSetService.updateById(hospitalSet);
        return R.ok();
    }

}

